package com.fare.taluo.application.impl.analyse;

import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.foreveross.hbase.client.HBaseHelper;
import org.foreveross.hbase.client.HBasePlaneinfoHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.fare.taluo.application.LowestPriceQueryApplication;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.application.analyse.LowAnalyApplication;
/**
 * 首页报表查询接口
 * @author spring.华 
 * 	e-mail 13719279477@139.com
 */

@Transactional
@Scope("singleton")
@Service("lowAnalyApplication")
public  class LowerAnalyApplicationImpl implements LowAnalyApplication {

//		@Override
//		public List<PlaneInfo> earchChannel(String tableName, String startRowKey,
//				String endRowKey, List<Filter> filters) throws Exception {
//			List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
//		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
//		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
//		ResultScanner scan=HBaseHelper.getRecords(tableName, startRowKey, endRowKey,null,filters);
//		///HBaseHelper.printResult(scan);
//		list=HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
//		return list;
//	}
	public static void main(String[] args) throws Exception {
		String startRowKey="20000PEKSZX20130411";//海口
						  //20005CANSHA00CZ30002013010708
		String   endRowKey="20000PEKSZX201304170";//
		LowerAnalyApplicationImpl imp=new LowerAnalyApplicationImpl();
//		 List dates=getDatesStr("2013-02-07","2013-02-15");
//		 List<Filter> filters=null;
//					String rowKeyHead="00002HAKWUH";//+DateUtils.getPageDateStr(vo.getDepTime(),"yyyyMMdd")
//					filters=getFilter(dates,rowKeyHead, "1");
					List<Filter> filters=new ArrayList();//00002HAKWUH2013021312 and v=0
					filters.add(new PrefixFilter("00002HAKWUH20130212".getBytes()));
					//filters.add(new PrefixFilter("00002HAKWUH201302101".getBytes()));

		List<PlaneInfo> list=imp.earchChannel("LP_LINE_DAY",startRowKey, endRowKey,null);
//		List dates=new ArrayList();
//		dates.add("2013-03-15");
//		List<PlaneInfo> ll=getMyLowsPlaneInfo(dates,list);
		for (PlaneInfo planeInfo : list) {
			System.out.println(planeInfo.getLowerPrice()+ planeInfo.getFlightBatchDate());
		}
//		System.out.println(list.size());
//		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
//			PlaneInfo planeInfo = (PlaneInfo) iterator.next();
//			System.out.println(planeInfo.getLowprPrice());
//		}
		
	}
	
	
	public static List<PlaneInfo>  getMyLowsPlaneInfo(List<String> dateList ,List<PlaneInfo> myList){
		///比较出渠道的最低价
		List<PlaneInfo> dataList=new ArrayList<PlaneInfo>();
		for(int i=0;i<dateList.size();i++){//天数
			String date=dateList.get(i);
			float lower=0f;
			PlaneInfo info=new PlaneInfo();
			//for (Iterator iterator = qudaoList.iterator(); iterator.hasNext();) {
				///List<PlaneInfo> infos = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = myList.iterator(); iterator2
						.hasNext();) {
					PlaneInfo pinfo = (PlaneInfo) iterator2.next();
					if(StringUtils.isEmpty(pinfo.getLowerPrice())){
						continue;
					}
					if(pinfo.getFlightBatchDate().equals(date)){
						if(lower==0f&&StringUtils.isNotEmpty(pinfo.getLowerPrice())){
							info=pinfo;
							lower=1f;
						}else if(StringUtils.isEmpty(pinfo.getLowerPrice())){
							continue;
						}
						else{//进行比较
							if(Float.parseFloat(info.getLowprPrice()+"")>Float.parseFloat(pinfo.getLowprPrice())){
								info=pinfo;
							}
						}
						///break;
					}
				}
		//	}
			if(info!=null&&StringUtils.isNotEmpty(info.getLowerPrice())){
				dataList.add(info);
			}
		}	
		
		return dataList;
	}


	@Override
	public List<PlaneInfo> earchChannel(String tableName, String startRowKey,
			String endRowKey, List<Filter> filters) throws Exception {
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
	//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
	//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
	ResultScanner scan=HBaseHelper.getRecords(tableName, startRowKey, endRowKey,null,filters);
	///HBaseHelper.printResult(scan);
	//System.out.println("++++++tableName++++++"+tableName+"+++++++++startRowKey++++++++"+startRowKey+"++++++++endRowKey+++++"+endRowKey);
	list=HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
	//System.out.println("+++list.size()++++"+list.size());
	return list;
		
	}
	
	

}
